package com.dormitory.dormitorymanagement.service.ipml;

import com.dormitory.dormitorymanagement.mapper.AdminMapper;
import com.dormitory.dormitorymanagement.mapper.StudentsMapper;
import com.dormitory.dormitorymanagement.pojo.*;
import com.dormitory.dormitorymanagement.pojo.dto.DormRepairDto;
import com.dormitory.dormitorymanagement.pojo.dto.LoginDto;
import com.dormitory.dormitorymanagement.pojo.dto.NoticeDto;
import com.dormitory.dormitorymanagement.pojo.dto.UserDto;
import com.dormitory.dormitorymanagement.service.AdminService;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
public class AdminServiceIpml implements AdminService {
    /**
     * 登录
     * @param loginDto
     * @return
     */


@Autowired
private AdminMapper adminMapper;
    @Autowired
    private StudentsMapper studentsMapper;

    /**
     * 报修列表
     * @return
     */
    public PageBean<DormRepair> repairList(DormRepairDto dormRepairDto) {
        PageBean<DormRepair> pb = new PageBean<>();
        //开启分页查询
        PageHelper.startPage(dormRepairDto.getPage(),dormRepairDto.getPageSize());

        List<DormRepair> as = adminMapper.repairList(dormRepairDto.getStartDate(),dormRepairDto.getEndDate(),dormRepairDto.getDormitory(),dormRepairDto.getAdminId());
        System.out.println(as);
        Page<DormRepair> p = (Page<DormRepair>) as;
//        把数据填充进page对象中
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return pb;
    }


    /**
     * 登录
     * @param loginDto
     * @return
     */
    public DormitoryAdmin login(LoginDto loginDto) {
        return adminMapper.findByStaffId(loginDto.getUsername());
    }

    /**
     * 修改报修单状态
     * @param repairId
     * @param status
     */
    public void updateStatus(Integer repairId, String status) {
        adminMapper.updateRepairStatus(repairId,status);
    }

    /**
     *  公告
     * @param noticeDto
     */
    public void notice(NoticeDto noticeDto) {
        Notice notice = new Notice();
         notice.setTitle(noticeDto.getTitle());
         notice.setContent(noticeDto.getContent());
          notice.setPublishTime(noticeDto.getPublishTime());
          notice.setStaffId(noticeDto.getStaffId());
        DormitoryAdmin dormAdmin = adminMapper.findByStaffId(noticeDto.getStaffId());
         notice.setStaffName(dormAdmin.getName());
         notice.setStaffName( dormAdmin.getBuildingResponsible());
          notice.setCreateTime(LocalDateTime.now());
          notice.setUpdateTime(LocalDateTime.now());
        adminMapper.notice(notice);
    }

    /**
     * 获取同学的评价
     * @param studentId
     * @param page
     * @param pageSize
     * @return
     */
    public PageBean<StudentEvaluations> getStudentClassmateEvaluations(String studentId, Integer page, Integer pageSize) {
        PageBean<StudentEvaluations> pb = new PageBean<>();

        // 开启分页查询
        PageHelper.startPage(page, pageSize);

        // 执行分页查询
        List<StudentEvaluations> as = adminMapper.getStudentClassmateEvaluations(studentId);


        // 使用PageInfo包装查询结果，获取分页信息
        PageInfo<StudentEvaluations> pageInfo = new PageInfo<>(as);

        // 填充数据到PageBean
        pb.setItems(pageInfo.getList());
        pb.setTotal(pageInfo.getTotal());

        return pb;
    }

    /**
     * 管理员注册
     * @param userDto
     */
    public void adminRedister(UserDto userDto) {
        DormitoryAdmin dormAdmin = new DormitoryAdmin();
        dormAdmin.setStaffId(userDto.getAdminId());
        dormAdmin.setName(userDto.getName());
        dormAdmin.setBuildingResponsible(userDto.getBuilding());
        dormAdmin.setPassword(userDto.getPassword());
        dormAdmin.setUsername(userDto.getUsername());
        dormAdmin.setRole("宿管");
        dormAdmin.setCreateTime(LocalDateTime.now());
        dormAdmin.setUpdateTime(LocalDateTime.now());

        adminMapper.adminRedister(dormAdmin);
    }
}
